package com.menker.app.webview;


import android.annotation.TargetApi;
import android.net.http.SslError;
import android.os.Build;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import myandroid.library.util.WebViewLoadHelper;

public class BaseWebViewClient extends WebViewClient {
    private String mLoadUrl = "";

    public BaseWebViewClient(String loadUrl) {
        this.mLoadUrl = loadUrl;
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
//        Timber.d("Failing url: " + failingUrl + "\n"
//                + "ErrorCode: " + errorCode + "\n"
//                + "Description: " + description);
        webView.loadDataWithBaseURL("", WebViewLoadHelper.getLoadErrorMessage(mLoadUrl), "text/html", "UTF-8","");
    }

    @TargetApi(Build.VERSION_CODES.M)
    @Override
    public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError err) {
        // Redirect to deprecated method, so you can use it in all SDK versions
        if (Build.VERSION.SDK_INT >= 21) {
            onReceivedError(view, err.getErrorCode(), err.getDescription().toString(), req.getUrl().toString());
        } else {
            onReceivedError(view, 0, "低版本的设备调用了高版本的API", req.getUrl().toString());
        }
    }

    @Override
    public void onReceivedSslError(WebView webView, SslErrorHandler sslErrorHandler, SslError sslError) {
        // sslErrorHandler.cancel(); // Android默认的处理方式
        sslErrorHandler.proceed();  // 接受所有网站的证书
    }

    //    @Override
//    public WebResourceResponse shouldInterceptRequest(WebView webView, String url) {
//        WebResourceResponse response = null;
//        // 检查该资源是否已经提前下载完成。我采用的策略是在应用启动时，用户在 wifi 的网络环境下
//        // 提前下载 H5 页面需要的资源。
//        boolean resDown = JSHelper.isURLDownValid(url);
//        if (resDown) {
//            jsStr = JsjjJSHelper.getResInputStream(url);
//            if (url.endsWith(".js") && jsStr != null) {
//                response = getWebResourceResponse("text/javascript", "UTF-8", ".js");
//            } else if (url.endsWith(".css") && jsStr != null) {
//                response = getWebResourceResponse("text/css", "UTF-8", ".css");
//            } else if (url.endsWith(".html") && jsStr != null) {
//                response = getWebResourceResponse("text/html", "UTF-8", ".html");
//            }
//        }
//        // 若 response 返回为 null , WebView 会自行请求网络加载资源。
//        return response;
//    }
//
//    @Override
//    public WebResourceResponse shouldInterceptRequest(WebView webView, WebResourceRequest webResourceRequest) {
//        shouldInterceptRequest(webView, webResourceRequest.getUrl().toString());
//    }
//
//    public String getJsjjJSPath() {
//        String splashTargetPath = JarEnv.sApplicationContext.getFilesDir().getPath() + "/JS";
//        if (!TPFileSysUtil.isDirFileExist(splashTargetPath)) {
//            TPFileSysUtil.createDir(splashTargetPath);
//        }
//        return splashTargetPath + "/";
//    }
//
//    private WebResourceResponse getWebResourceResponse(String url, String mime, String style) {
//        WebResourceResponse response = null;
//        try {
//            response = new WebResourceResponse(mime, "UTF-8", new FileInputStream(new File(getJSPath() + TPMD5.md5String(url) + style)));
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
//        return response;
//    }
}
